package ltd.hyangyang.rpc.core.provider.impl;

import io.netty.util.internal.StringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import ltd.hyangyang.rpc.common.bean.RpcConfig;
import ltd.hyangyang.rpc.common.bean.RpcProperties;
import ltd.hyangyang.rpc.common.constant.RegistryEnum;
import ltd.hyangyang.rpc.common.exception.ExceptionEnum;
import ltd.hyangyang.rpc.common.exception.RpcException;
import ltd.hyangyang.rpc.common.extension.ExtensionLoader;
import ltd.hyangyang.rpc.common.utils.SingletonFactory;
import ltd.hyangyang.rpc.core.provider.ProviderService;
import ltd.hyangyang.rpc.core.registory.DiscoverService;
import ltd.hyangyang.rpc.core.registory.RegistryService;
import ltd.hyangyang.rpc.core.registory.zook.CuratorUtils;
import ltd.hyangyang.rpc.core.registory.zook.ZookDisCoverService;
import ltd.hyangyang.rpc.core.registory.zook.ZookRegistryService;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.logging.Logger;

@Slf4j
public class ProviderServiceImpl implements ProviderService {

    public RpcProperties rpcProperties;

    public RegistryService registryService;

    public Logger logger = Logger.getLogger("debug");

    @SneakyThrows
    public ProviderServiceImpl(){
        rpcProperties = SingletonFactory.getSingletonBean(RpcProperties.class);
//        ServiceLoader<RegistryService> registryServices = ServiceLoader.load(RegistryService.class);
//        Iterator<RegistryService> iterator = registryServices.iterator();

        ExtensionLoader<RegistryService> extensionLoader = ExtensionLoader.getExtensionLoader(RegistryService.class);

        registryService = extensionLoader.getExtension();
//        while (iterator.hasNext()){
//            RegistryService registryService = iterator.next();
//            String canonicalName = registryService.getClass().getCanonicalName();
//            String registryType = rpcProperties.getRegistryType();
//            if (registryType.equals(RegistryEnum.ZOOKEEPER.registryType)&&ZookRegistryService.class.getCanonicalName().equals(canonicalName)){
//                //如果使用的注册中心为zookeeper
//                this.registryService = registryService;
//
//            }else if (registryType.equals(RegistryEnum.NACOS)){
//                //TODO 判断条件待补充,使用nacos作为注册中心
//                this.registryService = registryService;
//
//            }
//        }
    }

    @Override
    public void publishService(RpcConfig rpcConfig) throws Exception {
        //注册消息
        registryService.registry(rpcConfig);
    }
}
